package com.yyge.web.servlet;

import com.yyge.dto.GoodsInfoDto;
import com.yyge.entity.GoodsInfo;
import com.yyge.service.GoodsInfoService;
import com.yyge.service.impl.GoodsInfoServiceImpl;
import com.yyge.util.ServletUtil;
import com.yyge.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jws.WebService;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet("/goods/add")
public class AddGoodsInfoServlet extends HttpServlet {
    private GoodsInfoService service=new GoodsInfoServiceImpl();
    private static Logger logger= LoggerFactory.getLogger(SearchGoodsInfoServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        GoodsInfoDto goodsInfoDto= ServletUtil.parameter2dto(req,GoodsInfoDto.class);
        R r=service.selectAll(goodsInfoDto);

        Object pageInfo=r.getData();

        // 将pageVo保存到request作用域中
        // 再做一次页面转发，将request请求转发到另外的用于展示数据的jsp页面中
        // jsp页面只复制遍历，展示数据

        req.setAttribute("pageInfo",pageInfo);
         // 用户不能够在浏览器中访问到WEB-INF目录中的文件
        RequestDispatcher requestDispatcher=req.getRequestDispatcher("/WEB-INF/pages/goodsAdd.jsp");
        // 页面转发
        requestDispatcher.forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      logger.debug("reg servlet接收到客户端发起Post请求");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        //一个迭代器
        Enumeration<String> parameterNames= req.getParameterNames();
        GoodsInfoService service=new GoodsInfoServiceImpl();
       // GoodsInfo goodsInfo=new GoodsInfo();

        GoodsInfo goodsInfo=new GoodsInfo();
//        goodsInfo.setGoodsName("水果-香蕉");
//        goodsInfo.setGoodsSerial("霸王香蕉");
//        goodsInfo.setGoodsImg("img");
//        goodsInfo.setGoodsSort(3);
//        goodsInfo.setTypeParentId(66);
//        goodsInfo.setTypeMiddleId(77);
//        goodsInfo.setTypeSubId(88);
//        goodsInfo.setStoreNumber(66);
//        goodsInfo.setSelledNumber(88);
//        goodsInfo.setWarnNumber(99);
//        goodsInfo.setMarketPrice(66.66);
//        goodsInfo.setShopPrice(99.99);
//        goodsInfo.setGoodsEnable(1);
//        goodsInfo.setSummary("好东西哦");
//        goodsInfo.setIsnew(1);
//        goodsInfo.setIshot(1);
//        goodsInfo.setIsrecommand(1);

        while (parameterNames.hasMoreElements()){
         String parameter=parameterNames.nextElement();
         String value=req.getParameter(parameter);
          if(parameter.equals("goodsName")){goodsInfo.setGoodsName(value);}
          if(parameter.equals("goodsSerial")){goodsInfo.setGoodsSerial(value);}
          if(parameter.equals("storeNumber")){goodsInfo.setStoreNumber(Integer.parseInt(value));}
          if(parameter.equals("selledNumber")){goodsInfo.setSelledNumber(Integer.parseInt(value));}
          if(parameter.equals("shopPrice")){goodsInfo.setShopPrice((Double.valueOf(value)));}
          if(parameter.equals("marketPrice")){goodsInfo.setMarketPrice((Double.valueOf(value)));}
          goodsInfo.setGoodsSort(99);
          logger.debug("接收客户端发起的post请求， 参数 {}, 值 {}", parameter, value);
        }

        R r=service.insert(goodsInfo);
    }
}
